<?php
header("Content-type:text/html; charset='utf8'");

set_time_limit(0);

require_once("mysql_pdo.php");

require_once("model.php");

require_once("Snoopy.class.php");

//采集该种类别START****************************************

//define source array $sourceArr
$sourceArr = array();

//initialize the array $sourceArr
//key:ganchi_id which be set by www.ganchi.net
//value:us_category which be set by us


$sourceArr = array(
'95'=>'75','102'=>'76','138'=>'77','152'=>'78','140'=>'79',
'151'=>'80','97'=>'81','139'=>'82','99'=>'83','125'=>'84','101'=>'85',
'125'=>'86','144'=>'87','129'=>'88','131'=>'89','126'=>'90','128'=>'91',
'130'=>'92','163'=>'93','132'=>'94'
); 

/* $sourceArr = array(
'31'=>'1','34'=>'2','35'=>'3','46'=>'4','32'=>'5','37'=>'6',
'39'=>'7','41'=>'8','45'=>'9','48'=>'10','36'=>'11','30'=>'12',
'33'=>'13','38'=>'14','40'=>'15','103'=>'16','111'=>'17','105'=>'18',
'47'=>'19','49'=>'20','107'=>'21','50'=>'22','52'=>'23','109'=>'24',
'54'=>'25','56'=>'26','169'=>'27','143'=>'28','104'=>'29','51'=>'30',
'53'=>'31','106'=>'32','55'=>'33','116'=>'34','57'=>'35','108'=>'36',
'60'=>'37','38'=>'38','63'=>'39','58'=>'40','136'=>'41','62'=>'42',
'111'=>'43','65'=>'44','113'=>'45','68'=>'46','156'=>'47','115'=>'48',
'69'=>'49','112'=>'50','67'=>'51','114'=>'52','59'=>'53','61'=>'54',
'120'=>'55','64'=>'56','118'=>'57','66'=>'58','121'=>'59','87'=>'60',
'117'=>'61','89'=>'62','92'=>'63','119'=>'64','165'=>'65','88'=>'66',
'90'=>'68','94'=>'69','122'=>'70','168'=>'71','124'=>'72',
'96'=>'73','123'=>'74','95'=>'75','102'=>'76','138'=>'77','152'=>'78','140'=>'79',
'151'=>'80','97'=>'81','139'=>'82','99'=>'83','125'=>'84','101'=>'85',
'125'=>'86','144'=>'87','129'=>'88','131'=>'89','126'=>'90','128'=>'91',
'130'=>'92','163'=>'93','132'=>'94'
);  */
//循环调用函数采集
foreach($sourceArr as $ganchi_id=>$us_category)
{
	crawlStart($ganchi_id,$us_category,$dbh);
	echo "<hr/>";
}

//destroy $sourseArr Array
unset($sourceArr);

//crawlStart function start
function crawlStart($ganchi_id,$us_category,$dbh)
{
	$snoopy = new Snoopy();

	$urlLink = "http://www.ganchi.net/category.php?id="."$ganchi_id";

	$snoopy->fetchlinks($urlLink);   
		
	$url=array(); 
	  
	$url=$snoopy->results; 


	//该种分类翻页的个数:$urlPageCount
	$urlPage = ganchi_getFoodHasHowManyPage($url);
	$urlPageSingle = ganchi_array_multi_unique($urlPage); 
	(int)$urlPageCount = count($urlPageSingle) + 1;

	//销毁数组
	unset($surl);
	unset($urlPage);
	unset($urlPageSingle);

	//遍历每个具体翻页页面
	for($i=1;$i<=$urlPageCount;$i++)
	{
		$snoopy = new Snoopy();   
		$snoopy->fetchlinks("http://www.ganchi.net/category.php?id=".$ganchi_id."&page=".$i."&sort=comment_rank&order=DESC");   
		$urlLinks = array();   
		$urlLinks = $snoopy->results;   
		
		//该种食品每个翻页页面包含的所有具体页面的链接：$urlDetail
		//deleteUnlinkUrl 去除不相关的链接
		//array_multi_unique 合并数组值相等的数组元素
		$urlDetail = ganchi_deleteUnlinkUrl($urlLinks);
		$urlDetailHandle = ganchi_array_multi_unique($urlDetail);   
		//print_r($urlDetailHandle);
		//销毁数组
		unset($urlDetail);
		
		//该种分类在该翻页某一具体页面的详细信息
		foreach($urlDetailHandle as $key=>$value)
		{
			//获取页面所有信息
			$content = file_get_contents($value);
			//print_r($content);
			
			//匹配标题 $title:未处理的食物标题,可能含有（）;$titleHandle:处理后的食物标题
			if(preg_match_all("/<h1>(.*?)<\/h1>/is",$content,$title))
			{
				//delete（）or ()
				if(preg_match_all("/(.*?)\（(.*?)\）/is",$title[1][0],$titl))
				{
					$titleHandle = DeleteHtml($titl[1][0]);
				}
				elseif(preg_match_all("/(.*?)\((.*?)\)/is",$title[1][0],$tit))
				{
					$titleHandle = DeleteHtml($tit[1][0]);
				}
				else
				{
					$titleHandle = DeleteHtml($title[1][0]);
				}
			}
			
			//销毁食物信息数组
			unset($title);
			unset($titl);
			
			//匹配品牌和生产商
			if(preg_match_all("/<ul id=\"summary\"(.*?)<\/ul>/is",$content,$brandProduce))
			{
				if(preg_match_all("/<li>(.*?)<\/li>/is",$brandProduce[1][0],$brandProduceLi))
				{
					//print_r($brandProduceLi);
					$brandHandle = DeleteHtml($brandProduceLi[0][0]);
					$ProduceHandle = DeleteHtml($brandProduceLi[0][1]);
					$brand = substr($brandHandle,9);
					$Produce = substr($ProduceHandle,12);
				}//end if li
			}//end if ul

			//将食物名称存入food_basic表,返回其basic_id
			$dbFoodBasicId = ganchi_saveFoodBasicInfo($titleHandle,$us_category,$brand,$Produce,$dbh);
			 
			//销毁品牌和生产商数组
			unset($brandProduce);
			unset($brandProduceLi);
			
			//匹配图片
			if(preg_match_all("/<div id=\"demo1\"(.*?)<\/div>/is",$content,$picDiv))
			{
				if(preg_match_all("/<li>(.*?)<\/li>/is",$picDiv[1][0],$picLi))
				{
					foreach($picLi[1] as $key=>$value)
					{
						if(preg_match_all("/src=\"(.*?)\" width=\"50\"/is",$value,$picImg))
						{
							$ganchi_pic = DeleteHtml($picImg[1][0]);
							$url = "http://www.ganchi.net/$ganchi_pic";
							$filepath = "../imageTemp";
							$img = ganchi_getImages($url,$dbFoodBasicId,$filepath,$dbh) ;
							
							echo '<pre><img src="'.$img.'"></pre>';echo "<br/>";
						}//end if img 
					}//end foreach
				}//end if li
			}//end if div
			 
			//销毁图片数组
			unset($picDiv);
			unset($picImg);

			//匹配评论
			if(preg_match_all("/<ul id=\"i-detail2\"(.*?)<\/ul>/is",$content,$commentUl))
			{	
				if(preg_match_all("/<div class=\"comment_yys\">(.*?)<div class=\"fr blue3\"/is",$commentUl[1][0],$commentDiv1))
				{// print_r($commentDiv1);
					foreach($commentDiv1[1] as $key=>$value)
					{
						if(preg_match_all("/<span class=\"comment_txtr grey_l\">(.*?)<\/span>/is",$value,$commentSpan))
						{
								$comment = DeleteHtml($commentSpan[1][0]);
								$tip = DeleteHtml($commentSpan[1][1]);	

								//储存评论信息
								ganchi_saveComments($comment,$tip,$dbFoodBasicId,$dbh);
								
						}//end if
					}//end foreach
				}//end if div1
			}//end if ul
			
			//销毁评论数组
			unset($commentUl);
			unset($commentDiv1);
			unset($commentSpan);
			
			///////////////
			//匹配配料表
			if(preg_match_all("/<div class=\"changgui\">(.*?)<\/div>/is",$content,$peiliao))
			{
				if(preg_match_all("/<tr>(.*?)<\/tr>/is",$peiliao[0][0],$pl))
				{
					//print_r($pl[0]);
					foreach($pl[0] as $key=>$value)
					{
						if(preg_match_all("/<td class=\"grey_l\">(.*?)<\/td>/is",$value,$p))
						{
							//输出单项配料名
							//$batching = DeleteHtml($p[1][0]);
							
							//delete（） or ()
							if(preg_match_all("/(.*?)\（(.*?)\）/is",DeleteHtml($p[1][0]),$batching1))
							{
								$batching = $batching1[1][0];
							}
							elseif(preg_match_all("/(.*?)\((.*?)\)/is",DeleteHtml($p[1][0]),$batching2))
							{
								$batching = $batching2[1][0];
							}
							else
							{
								$batching = DeleteHtml($p[1][0]);
							}
							
							//存入food_batching表中,返回batching_id.结合food_basic_id,在food_basic_batching_rel建立联系
							$foodBatching = ganchi_saveBatchingInfo($batching,$dbh);
							
							//build relationship
							ganchi_buildRelationshipFoodBatching($dbFoodBasicId,$foodBatching,$dbh);
							
						}//end if td
					}//end foreach $pl[0]
				}//end if tr
			}//end if div
			
			//销毁食物配料数组
			unset($peiliao);
			unset($pl);
			unset($p);
			
			//匹配添加剂
			if(preg_match_all("/<div class=\"tianjiaji\">(.*?)<\/div>/is",$content,$additive))
			{
				if(preg_match_all("/<tr>(.*?)<\/tr>/is",$additive[0][0],$addi))
				{
					//print_r($addi[0]);
					foreach($addi[0] as $key=>$value)
					{
						if(preg_match_all("/<td class=\"grey_l\">(.*?)<\/td>/is",$value,$ad))
						{
							//输出单项添加剂名
							//echo $additiveHandle = DeleteHtml($ad[1][0]);
							
							//delete（） or ()
							if(preg_match_all("/(.*?)\（(.*?)\）/is",DeleteHtml($ad[1][0]),$additive1))
							{
								$additiveHandle = $additive1[1][0];
							}
							elseif(preg_match_all("/(.*?)\((.*?)\)/is",DeleteHtml($ad[1][0]),$additive2))
							{
								$additiveHandle = $additive2[1][0];
							}
							else
							{
								$additiveHandle = DeleteHtml($ad[1][0]);
							}

							//select this additive from `food_additive_category`,if this additive belong select results,then don't insert it to table of `food_additive` 
							$additiveExistsCount = getAdditiveExistsCounts($additiveHandle,$dbh);
							
							if($additiveExistsCount == 0)
							{
								//根据添加剂名查询food_additive表，返回其additive_id
								//根据additive_id和basic_id在food_basic_additive_rel表建立食物和添加剂的联系
								$dbAdditiveId = ganchi_getDbAdditiveId($additiveHandle,$dbh);
								ganchi_getDbAdditive($additiveHandle,$dbh);
								//rebuild relationship 
								ganchi_buildRelationshipFoodAdditive($dbFoodBasicId,$dbAdditiveId,$dbh);
							}
								
						}//end if td
					}//end foreach $addi[0]
				}//end if tr
			}//end if div
			
			//销毁食物添加剂数组
			unset($additive);
			unset($addi);
			unset($ad);
			
			//匹配营养成分
			if(preg_match_all("/<div class=\"yychengfen\">(.*?)<\/div>/is",$content,$nutrition))
			{
				//print_r($nutrition);
				if(preg_match_all("/<tr>(.*?)<\/tr>/is",$nutrition[0][0],$nutri))
				{
					//print_r($nutri);
					foreach($nutri[0] as $key=>$value)
					{
						//匹配比率值100g or 13g etc
						if(preg_match_all("/<th class=\"chengfenbg2\" width=\"40%\">(.*?)<\/th>/is",$value,$nu1))
						{
							//print_r($nu1);
							//输出单项营养成分名比率
							preg_match_all("/\d+/",DeleteHtml($nu1[1][0]),$arr);
							
							if(!empty($arr[0][0]))
							{
								$nutritionHandleRate = $arr[0][0];	
							}
							
						}//end if td chengfenbg1
						
						//匹配营养成分名
						if(preg_match_all("/ <td class=\"chengfenbg1\">(.*?)<\/td>/is",$value,$nu2))
						{
							//print_r($nu2);
							//输出单项营养成分名
							if(!empty($nu2[1][0]))
							{
								$nutritionHandleName = DeleteHtml($nu2[1][0]);							
							}
											
						}//end if td chengfenbg1
						
						//匹配营养成分实际含量值
						if(preg_match_all("/ <td class=\"chengfenbg2\">(.*?)<\/td>/is",$value,$nu3))
						{
							//print_r($nu3);
							//输出单项营养成分实际含量
							if(!empty($nu3[1][0]))
							{
								$nutritionHandleProPortion = DeleteHtml($nu3[1][0]);
							}	
						}//end if td chengfenbg2
						
						//匹配营养成分nrv
						if(preg_match_all("/ <td class=\"chengfenbg2\">(.*?)<\/td>/is",$value,$nu4))
						{
							//print_r($nu4);
							//输出单项营养成分比重数值
							if(!empty($nu4[1][0]))
							{
								$nutritionHandleNrv = DeleteHtml($nu4[1][1]);
							}							
						}//end if td chengfenbg2
						
						//将相关营养成分的信息存入food_nutrition表中
						ganchi_saveFoodNutritionInfo($dbFoodBasicId,$nutritionHandleRate,$nutritionHandleName,$nutritionHandleProPortion,$nutritionHandleNrv,$dbh);
						
						echo "<br>";
					}//end foreach nutri[0]
				}//end tr
			}//end if div
			
			//销毁食物营养成分数组
			unset($nutrition);
			unset($nutri);
			unset($nu1);
			unset($nu2);
			unset($nu3);
				
		}

		//销毁每个翻页的所有具体页面的url数组:$urlDetailHandle
		unset($urlDetailHandle);

	}//end for $i
}//end CrawlStart function
//采集该种类别END****************************************


	
// SELECT * FROM `food_category` WHERE `category`like '%粥%'
//update `food_maxuse` set `category_id`=216 WHERE `category_id`=139





?>


